package ks.server.controllers;

import ks.framework.common.Message;
import ks.framework.communicator.Communicator;
import ks.framework.debug.Debug;
import ks.framework.interfaces.IProcessMessage;

import org.w3c.dom.Document;

/**
 * Handle the login command for users.
 * 
 * @author George Heineman
 */
public class LoginController implements IProcessMessage {

	/** Primary agent to whom we can communicate our messages. */
	Communicator com;
	
	public LoginController (Communicator com) {
		this.com = com;
	}
	
	public boolean process(Message m) {
		Debug.println ("Observe login by: " + m.getOriginator());
		
		 // produce request to go to the client
        StringBuilder sb = new StringBuilder("<request version='1.0' id='1'><output><text>");
        sb.append(m.getOriginator()).append(" logged in");
        sb.append("</text></output></request>");
        Document d = Message.construct (sb.toString());
        
        // send to all clients...
        Message r = new Message (d);
        r.setBroadcast();
        
        com.distribute(r);
        return true;
	}
}